<template>
    
    <!-- 组件的模板 -->
    <div class="city-select">   
        <div class="weui-cells" v-if="isshow">
            <a class="weui-cell weui-cell_access" href="javascript:;" @click="getCurrAddress(did,dname)">
                <div class="weui-cell__bd">
                    <p>{{showname}}</p>
                </div>
                <div style="color: #bbb;font-size: 14px;line-height: 24px;">已选地区</div>
            </a>            
        </div>   
        <div class="weui-cells">            
            <a class="weui-cell weui-cell_access" href="javascript:;" 
                v-for="item in districtArr"
                :id="item.district_id"
                @click="getCurrAddress(item.district_id,item.short_name)">
                <div class="weui-cell__bd">
                    <p>{{item.short_name}}</p>
                </div>
            </a> 
        </div>
    </div>
        
</template>

<script>
    /**
     *  引入工具类库 或 Native API
     *  example : import widget from 'gapi/widget'
     */
    import * as util from 'js/util.js'
    import widget from 'gapi/widget.js'
    import cache from 'gapi/cache.js'
    import commonPage from 'clientApi/commonPage.js'
    /**
     *  引入组件 以及 组件接口(Action)
     *  example : import Somecomponents from 'components/Somecomponents/Somecomponents'
     */

    /**
     *  组件实例对象
     */
    export default {
        components: {

        },
        data: function(){
            return{
                isshow:true,
                showname:'',
                cid:'',
                cname:'',
                pid:'',
                pname:'',
                did:'',
                dname:'',
                districtArr:[],
                addressKey:{},
                addressVal:{}
            }
        },
        vuex: {
            getters: {

            },
            actions: {

            }
        },
        methods: {
            /**
             * 初始化
             * @method init
             * @public
             * @return {Null} void
             */
            init (){
                this.cid = util.getUrddataString('city_id') 
                this.cname = util.getUrddataString('city_name') 
                this.pid = util.getUrddataString('province_id') 
                this.pname = util.getUrddataString('province_name') 
                // 获取缓存里的数据
                cache.getCache('addressArrData', (data) => {
                    // alert(JSON.stringify(data))
                    if(data&&this.cname == data.value.city_name){
                        this.isshow = true
                        this.showname = data.value.district_name 
                        this.did = data.key.district_id
                        this.dname = data.value.district_name
                    }else{
                        this.isshow = false
                    }
                    
                })
            },
            //获取地址返回
            getCurrAddress (did,dname){
                this.addressKey = {
                    province_id:this.pid,
                    city_id:this.cid,
                    district_id:did
                }
                this.addressVal = {
                    province_name:this.pname,
                    city_name:this.cname,
                    district_name:dname
                }
                cache.getMemoryCache('webViewID',(data)=>{
                    if(data){
                        commonPage.goBack(1,{'key':this.addressKey,'value': this.addressVal},data.webViewID)
                    }
                })
            }
        },
        created(){
            /*获取地区下拉选择列表，存放在缓存里*/ 
            let myData = JSON.parse(localStorage.getItem("localityData")) 
            myData.forEach( (item) => {
                item.child.forEach( (itemChild) => {
                    if(itemChild.city_id == util.getUrddataString('city_id')){
                        this.districtArr = itemChild.child
                    }
                })
            })
            this.init();
            
        }
    }

    
</script>

<!-- CSS 样式 -->
<style src="../../node_modules/weui/dist/style/weui.css"></style>
<style src="../../static/css/common.css"></style>



